<!--  -->
<template>
  <div>
    <!-- 上传组件 -->
    <a-upload
      list-type="picture-card"
      :file-list="fileList"
      @preview="handlePreview"
      :disabled="disabled || loading"
      :customRequest="customRequest"
      :remove="remove"
      :beforeUpload="beforeUpload"
      :accept="accept"
    >
      <div v-if="fileList.length < limit && upload && !disabled">
        <a-icon :type="loading ? 'loading' : 'plus'" />
        <div class="ant-upload-text">上传图片</div>
      </div>
    </a-upload>
    <!-- 裁剪组件 -->
    <aCropper
      :cropperImg="cropperImg"
      @getCrofile="customRequest"
      @clear="clear"
      :optionWidth="optionWidth"
      :optionHeigth="optionHeigth"
      v-if="isCropper"
      :fixedBox="fixedBox"
    />
    <!-- 预览大图 -->
    <a-modal
      :visible="previewVisible"
      :footer="null"
      @cancel="previewVisible = false"
    >
      <img :alt="SYSTEM_TITLE" style="width: 100%" :src="previewImage" />
    </a-modal>
  </div>
</template>

<script>
//这里可以导入其他文件（比如：组件，工具js，第三方插件js，json文件，图片文件等等）
//例如：import 《组件名称》 from '《组件路径》';
import { getBase64 } from "@/utils/FileUtil";
import { SYSTEM_TITLE, IMAGE_UPLOAD_TYPE } from "@/config";
export default {
  name: "aImageSingle",
  model: {
    prop: "value",
    event: "change",
  },
  props: {
    /**
     * 允许上传的文件类型
     */
    accept: {
      type: String,
      default: IMAGE_UPLOAD_TYPE,
    },
    /**
     * 是否禁用,禁用后，上传，排序，删除都不会显示操作
     */
    disabled: { type: Boolean, default: false },
    /**
     * 是否可上传
     */
    upload: {
      type: Boolean,
      default: true,
    },
    /**
     * 文件列表
     */
    value: {
      type: String,
      default: "",
    },
    /**
     * 是否需要裁剪
     */
    isCropper: {
      type: Boolean,
      default: false,
    },
    /**
     * 裁剪框宽
     */
    optionWidth: {
      type: Number,
      String,
      default: 900,
    },
    /**
     * 裁剪框高
     */
    optionHeigth: {
      type: Number,
      String,
      default: 600,
    },
    // 固定截图框的大小
    fixedBox: {
      type: Boolean,
      default: false,
    },
  },
  //import引入的组件需要注入到对象中才能使用
  components: {},
  data() {
    //这里存放数据
    return {
      api: {},
      SYSTEM_TITLE,
      previewVisible: false, // 预览是否打开
      previewImage: "",
      fileList: [],
      limit: 1,
      loading: false,
      cropperImg: "",
    };
  },
  //监听属性 类似于data概念
  computed: {},
  //监控data中的数据变化
  watch: {
    /**
     * 文件地址统一处理前缀
     * @param {*} data
     */
    fileList(data) {
      if (data && data[0]) {
        this.$emit("change", data[0].uid);
        this.$emit("changeValue", data[0].uid);
      } else {
        this.$emit("change", "");
        this.$emit("changeValue", "");
      }
    },
    value() {
      this.getFileList();
    },
  },
  //方法集合
  methods: {
    clear() {
      this.cropperImg = "";
    },
    beforeUpload(file) {
      if (!this.isCropper) {
        return true;
      }
      this.cropperImg = URL.createObjectURL(file);
      return false;
    },
    /**
     * 回显
     */
    getFileList() {
      this.fileList = [];
      if (this.value) {
        this.fileList.push({
          uid: this.value,
          name: this.value,
          status: "done",
          url: this.$filePrefix + this.value,
        });
      }
    },
    /**
     * 预览
     * @param {*} file
     */
    async handlePreview(file) {
      if (!file.url && !file.preview) {
        file.preview = await getBase64(file.originFileObj);
      }
      this.previewImage = file.url || file.preview;
      this.previewVisible = true;
    },
    /**
     * 删除
     * @param {} file
     */
    remove(file) {
      this.fileList = this.fileList.filter((x) => x.uid !== file.uid);
      return true;
    },
    /**
     * 自定义上传
     * @param {} data
     */
    async customRequest(data) {
      this.loading = true;
      this.cropperImg = "";
      let result = await this.$commonjs.upload.uploadSingle(data.file);
      if (result) {
        this.fileList.push({
          uid: result.uuid,
          name: result.newFileName,
          status: "done",
          url: this.$filePrefix + result.uuid,
        });
      }
      this.loading = false;
    },
  },
  //生命周期 - 创建完成（可以访问当前this实例）
  created() {},
  //生命周期 - 挂载完成（可以访问DOM元素）
  mounted() {
    this.getFileList();
  },
  beforeCreate() {}, //生命周期 - 创建之前
  beforeMount() {}, //生命周期 - 挂载之前
  beforeUpdate() {}, //生命周期 - 更新之前
  updated() {}, //生命周期 - 更新之后
  beforeDestroy() {}, //生命周期 - 销毁之前
  destroyed() {}, //生命周期 - 销毁完成
  activated() {}, //如果页面有keep-alive缓存功能，这个函数会触发
};
</script>
<style>
/* you can make up upload button and sample style by using stylesheets */
.ant-upload-select-picture-card i {
  font-size: 32px;
  color: #999;
}

.ant-upload-select-picture-card .ant-upload-text {
  margin-top: 8px;
  color: #666;
}
</style>
